package com.mingtu.guards.adapter;


import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.mingtu.guards.R;
import com.youth.banner.adapter.BannerAdapter;

import java.util.List;

/**
 * @ClassName :
 * @Author : wkk
 * @Date : 2021-5-31 17:05:29
 * @Email : 1281573890@qq.com
 * @Eescription :
 */

/**
 * 自定义布局，图片
 */
public class NoticeBannerAdapter extends BannerAdapter<String, NoticeBannerAdapter.BannerViewHolder> implements View.OnClickListener {

    private OnItemClickListener mOnItemClickListener = null;
    private Context context;

    public NoticeBannerAdapter(Context context, List<String> mDatas) {
        //设置数据，也可以调用banner提供的方法,或者自己在adapter中实现
        super(mDatas);
        this.context = context;
    }

    //更新数据
    public void updateData(List<String> data) {
        //这里的代码自己发挥，比如如下的写法等等
//        mDatas.clear();
        mDatas.addAll(data);
        notifyDataSetChanged();
    }


    //创建ViewHolder，可以用viewType这个字段来区分不同的ViewHolder
    @Override
    public BannerViewHolder onCreateHolder(ViewGroup parent, int viewType) {
        // 实例化展示的view
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.layout_index_notice_view, parent, false);
        // 实例化viewholder
        ViewGroup.LayoutParams params = new ViewGroup.LayoutParams(
                ViewGroup.LayoutParams.MATCH_PARENT,
                ViewGroup.LayoutParams.MATCH_PARENT);
        view.setLayoutParams(params);
        BannerViewHolder viewHolder = new BannerViewHolder(view);
        return viewHolder;
    }

    @Override
    public void onBindView(BannerViewHolder holder, String data, int position, int size) {
        holder.tvNotice.setText(data + "");
    }

    @Override
    public void onClick(View v) {
        if (mOnItemClickListener != null) {
            //注意这里使用tag方法获取position
            mOnItemClickListener.onItemClick(v, (int) v.getTag());
        }
    }

    public void setOnItemClickListener(OnItemClickListener listener) {
        this.mOnItemClickListener = listener;
    }


    public static interface OnItemClickListener {
        void onItemClick(View view, int position);
    }

    public class BannerViewHolder extends RecyclerView.ViewHolder {
        public TextView tvNotice;
        private final View view;

        public BannerViewHolder(@NonNull View itemView) {
            super(itemView);
            view = itemView;
            this.tvNotice = (TextView) view.findViewById(R.id.tv_notice);
        }
    }

}
